<?php
declare(strict_types=1);

namespace App\Controller\Admin\V1\Rbac;

use App\Controller\Admin\AdminController;
use App\Model\MaterialAssign;
use App\Model\RbacPermissionHasSource;
use App\Model\RbacRoleHasPermission;
use App\Repositories\AdminInfoRepository;
use App\Repositories\AdminRepository;
use App\Repositories\AreaRepository;
use App\Repositories\CommunityRepository;
use App\Repositories\RbacMenuRepository;
use App\Repositories\StreetRepository;
use App\Request\StoreAdminRequest;
use App\Services\AdminService;
use Hyperf\Di\Annotation\Inject;

class ManagerController extends AdminController{
    /**
     * @Inject
     * @var AdminService
     */
    public $admin_service;
    public function all(){
        $search['id']=[
            'field' => 'id',
            'value' => '1',
            'method' => '!='
        ];
        $datas = AdminRepository::getInstance()->getList('*',$search,false);
        return $this->response->success($datas);
    }

    public function list(){
        $rows = $this->request->input('rows',10);
        $area_ids = $this->request->input('area_ids','');
        $real_name = $this->request->input('real_name','');
        $contact_tel = $this->request->input('contact_tel','');
        $search = [];
        $admin = $this->request->getAttribute('admin');
        $unit_id = $admin->unit_id;
        if($admin->id!=1){
            $search['id']=[
                'field' => 'id',
                'value' => '1',
                'method' => '!='
            ];
        }
        if($admin->district_id){
            $search['district_id']=[
                'field' => 'district_id',
                'value' => $admin->district_id
            ];
            $search['street_id']=[
                'field' => 'street_id',
                'value' => $admin->street_id
            ];
            $search['community_id']=[
                'field' => 'community_id',
                'value' => $admin->community_id
            ];
        }
        if($unit_id){
            $search['parent_id']=[
                'field' => 'parent_id',
                'value' => $unit_id
            ];
        }
        $area_ids = explode(',',$area_ids);
        if($count=count($area_ids)){
            if($count==1&&$area_ids[0]!=''){
                $search['district_id']=[
                    'field' => 'district_id',
                    'value' => $area_ids[0]
                ];
            }
            if($count==2){
                $search['district_id']=[

                    'field' => 'district_id',
                    'value' => $area_ids[0]
                ];
                $search['street_id']=[
                    'field' => 'street_id',
                    'value' => $area_ids[1]
                ];
            }
            if($count==3){
                $search['district_id']=[

                    'field' => 'district_id',
                    'value' => $area_ids[0]
                ];
                $search['street_id']=[
                    'field' => 'street_id',
                    'value' => $area_ids[1]
                ];
                $search['community_id']=[
                    'field' => 'community_id',
                    'value' => $area_ids[2]
                ];
            }
        }
        $join=[];
        if($contact_tel!=''||$real_name!=''){
            $join['admin_info'] = [
                'on' => ['admin.id','=','admin_info.admin_id'],
                'where'=>[]
            ];
        }
        if($contact_tel!=''){
            $search['contact_tel']=[
                'field' => 'contact_tel',
                'value' => $contact_tel,
                'method' => 'like'
            ];
        }

        if($real_name!=''){
            $search['real_name']=[
                'field' => 'real_name',
                'value' => $real_name,
                'method' => 'like'
            ];
        }

        $datas = AdminRepository::getInstance()->getList('admin.*',$search,['limit'=>$rows],[],$join);
        $district_ids =[];
        $street_ids=[];
        $community_ids = [];
        if(!empty($datas['result'])){
            foreach ($datas['result'] as &$item){
                $info = AdminInfoRepository::getInstance()->one(['id','contact_tel','real_name'],[[
                    'field' => 'admin_id',
                    'value' => $item['id']
                ]]);
                $item['info'] = $info?$info:new \stdClass();
                $item['district'] = $item['street'] = $item['community'] = new \stdClass();
                if($item['district_id']){
                    if(isset($district_ids[$item['district_id']])){
                        $item['district'] = $district_ids[$item['district_id']];
                    }else{
                        $district = AreaRepository::getInstance()->one(['id','name'],[[
                            'field' => 'id',
                            'value' => $item['district_id']
                        ]]);
                        $district_ids[$item['district_id']] = $district;
                        $item['district'] = $district;
                    }
                }
                if($item['street_id']){
                    if(isset($street_ids[$item['street_id']])){
                        $item['street'] = $street_ids[$item['street_id']];
                    }else{
                        $street = StreetRepository::getInstance()->one(['id','name'],[[
                            'field' => 'id',
                            'value' => $item['street_id']
                        ]]);
                        $street_ids[$item['street_id']] = $street;
                        $item['street'] = $street;
                    }
                }
                if($item['community_id']){
                    if(isset($community_ids[$item['community_id']])){
                        $item['community'] = $community_ids[$item['community_id']];
                    }else{
                        $community = CommunityRepository::getInstance()->one(['id','name'],[[
                            'field' => 'id',
                            'value' => $item['community_id']
                        ]]);
                        $community_ids[$item['community_id']] = $community;
                        $item['community'] = $community;
                    }
                }
            }
        }
        return $this->response->success($datas);
    }

    public function store(StoreAdminRequest $request){
        $params = $request->all();
        $admin = $this->request->getAttribute('admin');
        $params['status'] = isset($params['status'])&&$params['status']=='true'?1:0;
        if($admin->district_id||$admin->street_id||$admin->community_id){
            $params['role_id'] = $admin->role_id;
            $params['district_id'] = $admin->district_id;
            $params['street_id'] = $admin->street_id;
            $params['community_id'] = $admin->community_id;
        }
        $result = AdminRepository::getInstance()->store($params);
        if(!$result['success']){
            return $this->response->error(10055,$result['msg']);
        }
        return $this->response->success([
            'id' => $result['data']->id
        ]);
    }

    public function detail(){

    }

    public function delete(){

    }

    public function changeField()
    {
        $field = $this->request->input('field');
        $value = $this->request->input('value',false);
        $id = $this->request->input('id');
        $update =[
            $field=>$value=='true'?1:0
        ];
        $where=[
            [
                'field'=>'id',
                'value'=>$id
            ]
        ];
        $return = AdminRepository::getInstance()->update($update,$where);
        return $this->response->success([]);
    }

    public function getMenu(){
        $admin = $this->request->getAttribute('admin');
        if($admin->id==1){
            $menus = RbacMenuRepository::getInstance()->getList('*',[],false,[[
                'field' => 'listorder',
                'sort' => 'asc'
            ]]);
            return $this->response->success($menus);
        }
        $role_id = $admin->role_id;
        $role_has_permission_ids = RbacRoleHasPermission::query()->where('role_id',$role_id)->get()->pluck('permission_id')->toArray();
        if(empty($role_has_permission_ids)){
            return $this->response->success([]);
        }
        $permission_has_menu_ids = RbacPermissionHasSource::query()->whereIn('permission_id',$role_has_permission_ids)
            ->where('source_type','menu')->get()
            ->pluck('source_id')->toArray();
        if(empty($permission_has_menu_ids)){
            return $this->response->success([]);
        }
        $permission_has_menu_ids = array_unique($permission_has_menu_ids);
        $menus = RbacMenuRepository::getInstance()->getList('*',[[
            'field' => 'id',
            'value' => $permission_has_menu_ids,
            'method'=>'in'
        ]],false,[[
            'field'=>'listorder',
            'sort'=>'asc'
        ]]);
        return $this->response->success($menus);

    }
}